<?php
namespace app\client\controller;

use think\Url;
use think\Cache;
use think\Loader;
use think\Request;
use think\Session;
use think\Controller;
use app\console\model\GoodsType as GoodsTypeModel;
use app\common\trilateral\Classify as ClassifyAlias;
use app\client\model\ShopGoodsSpec as ShopGoodsSpecModel;
use app\client\model\ShopGoodsAttr as ShopGoodsAttrModel;

class ShopGoodsSpec extends Controller
{

    /**
     * 商品规格列表
     * @param ShopGoodsSpecModel $shopGoodsSpec
     * @return mixed
     */
    public function index(ShopGoodsSpecModel $shopGoodsSpec)
    {
        $data = $shopGoodsSpec->where(['member_id' => Session::get('shop_id')])->order(['id' => 'desc'])->select();


        return $this->fetch('', [
            'mate_title' => '商品规格列表',
            'data'       => $data
        ]);
    }


    /**
     * 新增商品规格
     * @param Request $request
     * @param ShopGoodsSpecModel $shopGoodsSpec
     * @return mixed
     */
    public function create(Request $request, ShopGoodsSpecModel $shopGoodsSpec)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('ShopGoodsSpec');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $shopGoodsSpec->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('ShopGoodsSpec/index'));
            }

            fail('操作失败');

        }


        return $this->fetch('', [
            "mate_title" => '新增商品规格',
            'data'       => ClassifyAlias::dimension(Cache::get('goodsType'))

        ]);

    }


    /**
     *删除商品规格
     * @param $id
     * @param ShopGoodsSpecModel $shopGoodsSpec
     */
    public function destroy($id, ShopGoodsSpecModel $shopGoodsSpec)
    {

        if ($shopGoodsSpec->amputate($id)) {
            win('操作成功', Url::build('ShopGoodsSpec/index'));
        }

        fail('操作失败');

    }


    /**
     * 删除全部商品规格
     * @param $id
     * @param ShopGoodsSpecModel $shopGoodsSpec
     */
    public function destroyAll($id, ShopGoodsSpecModel $shopGoodsSpec)
    {

        if ($shopGoodsSpec->amputateAll($id)) {
            win('操作成功', Url::build('ShopGoodsSpec/index'));
        }

        fail('操作失败');

    }


    /**
     * 编辑商品规格
     * @param $id
     * @param Request $request
     * @param ShopGoodsSpecModel $shopGoodsSpec
     * @return mixed
     */
    public function update($id, Request $request, ShopGoodsSpecModel $shopGoodsSpec)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('ShopGoodsSpec');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $shopGoodsSpec->allowField(true)->save($data, ['id' => $id]);

            if ($dispose) {
                win('操作成功', Url::build('ShopGoodsSpec/index'));
            }

            fail('操作失败');

        }

        $item = ShopGoodsSpecModel::get($id);

        return $this->fetch('create', [
            "mate_title" => '编辑商品规格',
            'data'       => ClassifyAlias::dimension(Cache::get('goodsType')),
            'item'       => $item

        ]);
    }


    /**
     *编辑商品状态
     * @param Request $request
     * @param ShopGoodsSpecModel $shopGoodsSpec
     * @return bool|int
     */
    public function auditing(Request $request, ShopGoodsSpecModel $shopGoodsSpec)
    {
        $id = $request->instance()->param('id');
        $status = $shopGoodsSpec->where('id', $id)->value('status') == '1' ? 2 : 1;

        if ($shopGoodsSpec->where('id', $id)->update(['status' => $status])) {

            return $status;
        }

        return false;
    }


    /**
     * 更新规格排序
     * @param Request $request
     * @param ShopGoodsSpecModel $shopGoodsSpec
     */
    public function sorting(Request $request, ShopGoodsSpecModel $shopGoodsSpec)
    {

        $id = $request->instance()->param('orderId/a');
        $sort = $request->instance()->param('sort/a');

        foreach ($id as $key => $value) {

            $shopGoodsSpec->where('id', $value)->update(['sort' => $sort[$key]]);

        }

        win('操作成功', Url::build('ShopGoodsSpec/index'));
    }


    /**
     * @param Request $request
     * @param ShopGoodsAttrModel $shopGoodsAttr
     * @return array
     */
    public function attrCreate(Request $request,ShopGoodsAttrModel $shopGoodsAttr)
    {

        $data = Request::instance()->param();

        $id = ShopGoodsAttrModel::insertGetId($data);

        if ($id) {
            return ['status' => 'true', 'value_id' => $id];
        }

        return ['status' => 'false'];
    }

    /*
     * 属性分类设置
     */
    public function attrList()
    {
        $inquire = Request::instance()->param();

        $condition['id'] = ['in', $inquire['attr_id']];
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];
        $data = GoodsTypeModel::where($condition)->field('id,title')->paginate('10');

        return $this->fetch('attrlist', [
            "mate_title" => '属性分类筛选',
            'data'       => $data
        ]);
    }

    /*
     * 属性设置
     */
    public function attrFlag()
    {

        if (Request::instance()->isPost()) {

            if (Request::instance()->param('title')) {

                // 提交数据
                (new ShopGoodsAttrModel())->allowField(true)->save(Request::instance()->param());

            }

            $ids = Request::instance()->param('ids/a');
            $flagName = Request::instance()->param('titleArr/a');

            for ($i = 0; $i < count($ids); $i++) {
                ShopGoodsAttrModel::where('id', $ids[$i])->update(['title' => $flagName[$i]]);
            }

        }

        $inquire = Request::instance()->param();
        $data = ShopGoodsAttrModel::where(['goods_type_id' => $inquire['goods_type_id'], 'spec_id' => $inquire['spec_id']])->select();

        return $this->fetch('attrflag', [
            "mate_title" => '属性设置',
            'data'       => $data
        ]);
    }

    /*
     * 属性删除
     */
    public function attrDel()
    {

        $inquire = Request::instance()->param();
        if (ShopGoodsAttrModel::destroy($inquire['id'])) {
            win('操作成功', Url::build('ShopGoodsSpec/attrFlag', ['goods_type_id' => $inquire['goods_type_id'], 'spec_id' => $inquire['spec_id']]));
        }

        fail('操作失败');

    }
}
